/**
 * Created by LeungZ on 2016/8/22.
 */
class Log {
    constructor(buildId, $modalInstance, appSrv, ctgNotification, $timeout, $window, $scope) {
        'ngInject';
        this._modalInstance = $modalInstance;
        this._appSrv = appSrv;
        this._timeout = $timeout;
        this._notification = ctgNotification;
        this.buildId = buildId;
        this.size = 0;
        this.logs = [];
        this.timer = "";

        this.getBuildLog();

        $timeout(function () {
            angular.element('.well.well-sm').css({
                'max-height': ($window.innerHeight - 190) + 'px',
                'overflow-y': 'scroll'
            });
        }, 0, false);

        $scope.$on('$destroy', ()=> {
            this._timeout.cancel(this.timer);
        });
    }

    getBuildLog() {
        this._appSrv.getBuildLog({id: this.buildId, size: this.size}).then(data=> {
            this.size = data.lastTimeFileSize;
            this.logs = this.logs.concat(data.log);
            if(data.isEnd === false || data.isEnd == 'false'){
                this.timer = this._timeout(this.getBuildLog.bind(this), 5000);
            }
            this._timeout(function(){
                angular.element('.well.well-sm').scrollTop(angular.element('.well.well-sm')[0].scrollHeight);
            },0,false);
        }).catch(()=> {
            this._timeout.cancel(this.timer);
            this._notification.error('获取日志数据有误!');
        });
    }

    cancel() {
        this._modalInstance.dismiss('cancel');
    }
}

export default Log